Imports System.IO
Imports System.Text

<ToolboxBitmap(GetType(TextBox))> _
Public Class ACTextBoxOpenFile
   Inherits TextBox

   Public WithEvents ACAyuda As Button
   Public Event ACAyudaClick(ByVal sender As Object, ByVal e As EventArgs)

   Private m_rellenaCeros As Boolean
   Private m_tipo As TipoTextBoxAyuda
   Private m_longitudtext As Integer = 0
   Private m_activarControl As Boolean = False
   Private m_archivo As Byte()
   Private m_nsdefaultfile As String
   Private m_nsfilter As String
   Private m_nsfilename As String
   Private m_nsfiledefined As Boolean
   Private m_nsfiledata As Byte()

   Public Enum TipoTextBoxAyuda
      Numerico
      AlfaNumerico
   End Enum

#Region " Constructores "
   Public Sub New()
      MyBase.New()
      MyBase.Size = New System.Drawing.Size(100, 21)

      Me.m_rellenaCeros = False
      Me.m_tipo = TipoTextBoxAyuda.AlfaNumerico

      ACAyuda = New System.Windows.Forms.Button
      ACAyuda.Image = Global.ACControles.My.Resources.Resources.folder_open
      ACAyuda.UseVisualStyleBackColor = True
      ACAyuda.Size = New System.Drawing.Size(21, 21)
      ACAyuda.Dock = DockStyle.Right
      ACAyuda.Cursor = Cursors.Arrow
      ACAyuda.TabStop = False

      MyBase.Controls.Add(ACAyuda)
      ACAyuda.BringToFront()

      m_nsfilter = "Todos los Archivos (*.*)|*.*"
      m_nsdefaultfile = "*"
   End Sub
#End Region

#Region " Propiedades "
   Public Property ACTipo() As TipoTextBoxAyuda
      Get
         Return m_tipo
      End Get
      Set(ByVal value As TipoTextBoxAyuda)
         m_tipo = value
      End Set
   End Property
   Public Property ACRellenaCeros() As Boolean
      Get
         Return m_rellenaCeros
      End Get
      Set(ByVal value As Boolean)
         m_rellenaCeros = value
      End Set
   End Property

   Public Property ACActivarAyudaAuto() As Boolean
      Get
         Return m_activarControl
      End Get
      Set(ByVal value As Boolean)
         m_activarControl = value
         ACAyuda.Enabled = Not value
      End Set
   End Property

   Public Property ACLongitudAceptada() As Integer
      Get
         Return m_longitudtext
      End Get
      Set(ByVal value As Integer)
         If Me.Text.Length >= value Then
            Me.Text = Me.Text.Substring(0, value)
            ACAyuda.Enabled = True
         Else
            ACAyuda.Enabled = False
         End If
         m_longitudtext = value
      End Set
   End Property

   Public Property ACArchivo() As Byte()
      Get
         Return m_archivo
      End Get
      Set(ByVal value As Byte())
         m_archivo = value
      End Set
   End Property

   Public Overrides Property Text() As String
      Get
         Return MyBase.Text
      End Get
      Set(ByVal value As String)
         MyBase.Text = value
      End Set
   End Property
   Public Overrides Property MaxLength() As Integer
      Get
         Return MyBase.MaxLength
      End Get
      Set(ByVal value As Integer)
         MyBase.MaxLength = value
      End Set
   End Property
   Public Overrides Property Cursor() As System.Windows.Forms.Cursor
      Get
         Return MyBase.Cursor
      End Get
      Set(ByVal value As System.Windows.Forms.Cursor)
         MyBase.Cursor = value
      End Set
   End Property
   Public Shadows Property Tag() As Object
      Get
         Return MyBase.Tag
      End Get
      Set(ByVal value As Object)
         MyBase.Tag = value
      End Set
   End Property

   Public Property ACDefaultFile() As String
      Get
         Return m_nsdefaultfile
      End Get
      Set(ByVal value As String)
         m_nsdefaultfile = value
      End Set
   End Property
   Public Property ACFilter() As String
      Get
         Return m_nsfilter
      End Get
      Set(ByVal value As String)
         m_nsfilter = value
      End Set
   End Property

   Public Property ACFileName() As String
      Get
         Return Path.GetFileName(m_nsfilename)
      End Get
      Set(ByVal value As String)
         m_nsfilename = value
      End Set
   End Property

   Public ReadOnly Property ACPathName() As String
      Get
         Return Path.GetDirectoryName(m_nsfilename)
      End Get
   End Property

   Public ReadOnly Property ACNameFileWithoutExtension() As String
      Get
         Return Path.GetFileNameWithoutExtension(m_nsfilename)
      End Get
   End Property

   Public ReadOnly Property ACFullPath() As String
      Get
         Return Path.GetFullPath(m_nsfilename)
      End Get
   End Property


   Public Property ACFileDefined() As Boolean
      Get
         Return m_nsfiledefined
      End Get
      Set(ByVal value As Boolean)
         m_nsfiledefined = value
      End Set
   End Property
   Public Property ACFileData() As Byte()
      Get
         Return m_nsfiledata
      End Get
      Set(ByVal value As Byte())
         m_nsfiledata = value
      End Set
   End Property

#End Region

#Region " Propiedades Protegidas "


   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      MyBase.Dispose(disposing)
   End Sub
   Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
      MyBase.OnGotFocus(e)
      If m_rellenaCeros Then
         MyBase.Text = Me.Text.PadLeft(Me.MaxLength, "0")
      End If
   End Sub
   Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
      MyBase.OnTextChanged(e)
   End Sub
   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
      Const WM_KEYDOWN As Integer = 256
      Const WM_SYSKEYDOWN As Integer = 260
      If ((msg.Msg = WM_KEYDOWN) _
                  OrElse (msg.Msg = WM_SYSKEYDOWN)) Then
         Select Case (keyData)
            Case Keys.Tab
               Dim e As New KeyEventArgs(Keys.Tab)
               MyBase.OnKeyDown(e)
         End Select
      End If
      Return MyBase.ProcessCmdKey(msg, keyData)
   End Function


   Protected Sub ACTextBoxAyuda_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
      If e.KeyCode = Keys.F4 Then
         If Me.Text.Trim().Length >= m_longitudtext And m_activarControl Then
            Me.ACAyuda_Click(sender, e)
         ElseIf Not m_activarControl Then
            Me.ACAyuda_Click(sender, e)
         End If
      End If
   End Sub
   Protected Sub ACTextBoxAyuda_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
      If m_tipo = TipoTextBoxAyuda.Numerico Then
         Dim numberFormatInfo As Globalization.NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
         Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
         Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
         Dim negativeSign As String = numberFormatInfo.NegativeSign

         Dim keyInput As String = e.KeyChar.ToString()

         If [Char].IsDigit(e.KeyChar) Then
            ' Digits are OK
         ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
            ' Decimal separator is OK
         ElseIf e.KeyChar = vbBack Then
            ' Backspace key is OK
         Else
            ' Swallow this invalid key and beep
            e.Handled = True
         End If
      End If
   End Sub
   Protected Sub ACAyuda_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ACAyuda.Click
      Try
         Dim _openFile As New OpenFileDialog()
         _openFile.DefaultExt = m_nsdefaultfile
         _openFile.FileName = m_nsfilename
         _openFile.Filter = m_nsfilter
         If _openFile.ShowDialog() = DialogResult.OK Then
            m_nsfilename = _openFile.FileName
            Me.Text = _openFile.FileName
            getFile(_openFile.FileName)
         End If
         RaiseEvent ACAyudaClick(sender, e)
      Catch ex As Exception
         ACDialogos.MostrarMensajeError("ACTextBoxOpenFile", "Ha Ocurrido un error al seleccionar el archivo.", ex)
      End Try
   End Sub

   Protected Sub ACActivarControlAutomatico(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
      If m_activarControl Then
         If Me.Text.Trim().Length >= m_longitudtext Then
            ACAyuda.Enabled = True
         Else
            ACAyuda.Enabled = False
         End If
      End If
   End Sub
#End Region

#Region " Metodos "
   Private Function ReadFile(ByVal sPath As String) As Byte()
      'Initialize byte array with a null value initially.
      Dim data As Byte() = Nothing

      'Use FileInfo object to get file size.
      Dim fInfo As New FileInfo(sPath)
      Dim numBytes As Long = fInfo.Length

      'Open FileStream to read file
      Dim fStream As New FileStream(sPath, FileMode.Open, FileAccess.Read)

      'Use BinaryReader to read file stream into byte array.
      Dim br As New BinaryReader(fStream)

      'When you use BinaryReader, you need to supply number of bytes to read from file.
      'In this case we want to read entire file. So supplying total number of bytes.
      data = br.ReadBytes(CInt(numBytes))
      Return data
   End Function

   Private Sub getFile(ByVal m_path As String)
      Try
         m_nsfiledata = ReadFile(m_path)
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
#End Region

End Class
